import Reflux from 'reflux'
import Action from '../action/action'

export default Reflux.createStore({
    listenables: [Action],


    /**首页**/
    bannerList: [],
    adList: [],
    homeProductList: [],

    //获取banner
    onGetBannerList(){
        util.request("banner.json",{},"GET",(res)=>{
            this.bannerList = res.data;
            this.trigger({bannerList: this.bannerList});
        });
    },
    //获取广告
    onGetAdList(){
        util.request("adList.json",{},"GET",(res)=>{
            this.adList = res.data;
            this.trigger({adList: this.adList});
        })
    },
    //获取推荐产品
    onGetHomeProductList(){
        util.request("recommendP.json",{},"GET",(res)=>{
            this.homeProductList = res.data;
            this.trigger({homeProductList: this.homeProductList})
        })
    },

    /**详情**/
    merchantList : [],
    productItem : {},
    onGetProductItem(i){
        this.productItem = this.homeProductList[i];
    },
    onSetProductItem(){
        this.trigger({productItem : this.productItem})
    },
    onGetMerchantList(callback){
       util.request("merchantList.json",{},"GET",(res)=>{
           this.merchantList = res.data;
           this.trigger({merchantList : this.merchantList},()=>{callback()})
       })
    },

    /**我的**/
    userInfo : {},
    userOrder : {},
    userWallet : {},
    navBarSelectIndex: 0,
    onGetUserInfo(){
        util.request("user.json",{},"GET",(res)=>{
            this.userInfo = res.data;
            this.trigger({userInfo: this.userInfo})
        })
    },
    onGetUserOrder(callback){
        util.request("userOrder.json",{},"GET",(res)=>{
            this.userOrder = res.data;
            this.trigger({userOrder: this.userOrder},()=>{callback()})
        })
    },
    //订单详情
    onSetNavBarSelectIndex(i){
        this.navBarSelectIndex = i;
    },
    onGetNavBarSelectIndex(){
        this.trigger({navBarSelectIndex:this.navBarSelectIndex})
    },
    //钱包
    onGetUserWallet(callback){
        util.request("userWallet.json",{},"GET",(res)=>{
            this.userWallet = res.data;
            this.trigger({userWallet:this.userWallet},()=>{callback()})
        })
    },
    onSetUserWallet(index){
        this.userWallet.cardList.map((obj,i)=>{
            obj.isDef = false;
            if(index === i){
                obj.isDef = true;
            };
            this.trigger({userWallet:this.userWallet})
        })
    },
    /**弹框**/
    checkBankListIndex:null,
    onCancelConcern(i){
        this.userInfo.userConcern.splice(i,1);
        this.trigger({userInfo: this.userInfo})
    },
    onSaveCheckBankListIndex(i){
        this.checkBankListIndex = i;
    },
    onGetCheckBankListIndex(){
        this.trigger({checkBankListIndex : this.checkBankListIndex})
    }
})